package com.social.msocial.frame;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import me.storm.ninegag.R;
import me.storm.ninegag.api.GagApi;
import me.storm.ninegag.data.GsonRequest;
import me.storm.ninegag.model.Feeds;
import me.storm.ninegag.model.Feeds.Feed;
import me.storm.ninegag.ui.adapter.CardsAnimationAdapter;
import me.storm.ninegag.ui.adapter.FeedsAdapter;
import me.storm.ninegag.ui.fragment.BaseFragment;
import me.storm.ninegag.util.TaskUtils;
import me.storm.ninegag.util.ToastUtils;
import me.storm.ninegag.view.LoadingFooter;
import me.storm.ninegag.view.OnLoadNextListener;
import me.storm.ninegag.view.PageStaggeredGridView;
/**
* Created by storm on 14-3-25.
*/
public class FeedFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
public static final String EXTRA_CATEGORY = "extra_category";
SwipeRefreshLayout mSwipeLayout;
PageStaggeredGridView gridView;
private MenuItem mRefreshItem;
private FeedsAdapter mAdapter;
private String mPage = "1";
String totalPage = "1";
private List<Feeds.Feed> list = new ArrayList<Feed>();
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
view = View.inflate(getActivity(), R.layout.fragment_feed_home, null);
mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
gridView = (PageStaggeredGridView) view.findViewById(R.id.grid_view);
mAdapter = new FeedsAdapter(getActivity(), list);
View header = new View(getActivity());
gridView.addHeaderView(header);
AnimationAdapter animationAdapter = new CardsAnimationAdapter(mAdapter);
animationAdapter.setAbsListView(gridView);
gridView.setAdapter(animationAdapter);
gridView.setLoadNextListener(new OnLoadNextListener() {
@Override
public void onLoadNext() {
try {
if(Integer.parseInt(totalPage)<Integer.parseInt(mPage)){
Toast.makeText(getActivity(), "最后一页", 0).show();
return;
}
loadNext();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
mSwipeLayout.setOnRefreshListener(this);
mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
android.R.color.holo_orange_light, android.R.color.holo_red_light);
loadFirst();
}
private void loadData(String next) {
if (!mSwipeLayout.isRefreshing() && ("1".equals(next))) {
setRefreshing(true);
}
// executeRequest(new GsonRequest(GagApi.LIST, Feeds.class,
// responseListener(), errorListener()));
Map<String, String> headers = new HashMap<String, String>();
headers.put("showCount", "10");
headers.put("currentPage", mPage);
executeRequest(new GsonRequest<Feeds>(Method.POST, GagApi.LIST, Feeds.class, headers, responseListener(),
errorListener()));
}
private Response.Listener<Feeds> responseListener() {
final boolean isRefreshFromTop = ("1".equals(mPage));
return new Response.Listener<Feeds>() {
@Override
public void onResponse(final Feeds response) {
System.out.println(response.toString());
totalPage = response.totalPage;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (isRefreshFromTop) {
list.clear();
list = response.hot_list;
mAdapter.setList(list);
mAdapter.notifyDataSetChanged();
mPage = String.valueOf(Integer.parseInt(mPage) 1);
setRefreshing(false);
} else {
if (list == null) {
list = new ArrayList<Feed>();
}
list.addAll(response.hot_list);
mAdapter.setList(list);
mAdapter.notifyDataSetChanged();
mPage = String.valueOf(Integer.parseInt(mPage) 1);
gridView.setState(LoadingFooter.State.Idle);
}
}
});
}
};
}
protected Response.ErrorListener errorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
ToastUtils.showShort(R.string.loading_failed);
setRefreshing(false);
gridView.setState(LoadingFooter.State.Idle, 3000);
}
};
}
private void loadFirst() {
gridView.setState(LoadingFooter.State.Idle);
mPage = "1";
loadData(mPage);
}
private void loadNext() {
loadData(mPage);
}
@Override
public void onRefresh() {
loadFirst();
}
private void setRefreshing(boolean refreshing) {
mSwipeLayout.setRefreshing(refreshing);
if (mRefreshItem == null)
return;
if (refreshing)
mRefreshItem.setActionView(R.layout.actionbar_refresh_progress);
else
mRefreshItem.setActionView(null);
}
}
评论